<?php

//use yii\web\View;

use yii\helpers\Url;

echo $this->render('/layouts/top');

?>
<script type="text/javascript" charset="utf-8" src="/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/ueditor/ueditor.all.js"> </script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" charset="utf-8" src="/js/ajax141212.js"></script>
<script type="text/javascript" src="/js/NoLimitCategory.js"></script>

<div style="margin:0px auto; width:1000px;">
	<div style=""><!--
 --><div style="display:inline-block;">分类：</div><!--
 --><div id="cont_category" style="display:inline-block;"><!--
--></div>
		<div style="margin:0px auto; width:1000px; margin-top:10px;">
			问题：<input id="question" type="text" value="<?php echo isset($knowledge["question"]) ? $knowledge["question"] : ""?>" style="width:400px;" />
		</div>
		<div style="margin-top:10px"><script id="editor" type="text/plain" style="width:959px;height:500px;"><?php echo isset($knowledge['answer']) ? stripslashes($knowledge['answer']) : "";?></script></div>
		<div style="margin:0px auto; width:1000px; margin-top:10px; text-align:center "><input type="button" value="发表文章" onclick="dataedit()"/></div>
	</div>
</div>
<br>
<?php echo $this->render('/layouts/footer');?>
<script language="javascript">

var id = <?php echo isset($knowledge["id"]) ? $knowledge["id"] : 0;?>;
var c1 = <?php echo isset($knowledge["c1"]) ? $knowledge["c1"] : 0;?>;
var c2 = <?php echo isset($knowledge["c2"]) ? $knowledge["c2"] : 0;?>;
var c3 = <?php echo isset($knowledge["c3"]) ? $knowledge["c3"] : 0;?>;


//实例化编辑器
//建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
var ue = UE.getEditor('editor', {
		toolbars: [
			['source','fontfamily', 'fontsize', 'bold', 'forecolor', 'insertunorderedlist', 'blockquote', 'horizontal', 'link', 'unlink','justifyleft', 'justifycenter', 'justifyright', 'removeformat', 'undo', 'redo', 'fullscreen']
		],
		autoHeightEnabled: true,
		autoFloatEnabled: true,
		imageScaleEnabled: false
});



var nlc = new NoLimitCategory(<?php echo json_encode($treeData, JSON_UNESCAPED_UNICODE)?>, document.getElementById("cont_category"), "全部");

nlc.onSelectCreate = function(selectObj, n){
	if(n == 1){
		selectObj.style.cssText = 'margin-left:0px;height:22px;';
	}else{
		selectObj.style.cssText = 'margin-left:5px;height:22px;';	
	}
};

var cids = [];
if(c1 != 0)cids.push(c1);
if(c2 != 0)cids.push(c2);
if(c3 != 0)cids.push(c3);

nlc.append(cids);


var dataedit = function(){

	var question = document.getElementById("question").value;
	var content = UE.getEditor('editor').getContent();
	
	var categorys = nlc.getSelects();
	var c1=0, c2=0, c3=0, cid = 0;
	for(var i=0; i<categorys.length; i++){
		if(i == 0)c1 = categorys[i].value;
		if(i == 1)c2 = categorys[i].value;
		if(i == 2)c3 = categorys[i].value;
		if(categorys[i].value > 0)cid = categorys[i].value;//所属分类为不为0的最后一个
	}

	new Ajax().post({
		url : "<?php echo Url::to(['/note/async-edit'])?>",
		data : "id="+id+"&c1="+c1+"&c2="+c2+"&c3="+c3+"&cid="+cid+"&question="+encodeURIComponent(question)+"&content="+encodeURIComponent(content),
		resultType : "json",
		onLoaded : function(result){
			console.log(result);
			if(result.code == 1){
				alert(result.info);
				
				id = (id>0) ? id : result.id;

				window.location.href = "<?php echo Url::to(['/note/edit'])?>?id="+id;
			}else{
				alert(result.info);
			}
			
		}
	});
}

	
</script>
</body>
</html>